package models

import (
	"fmt"
	"os"

	_ "github.com/Go-SQL-Driver/MySQL"
	"github.com/astaxie/beego/orm"

	"mutu/misc/conf"
)

var (
	Orm orm.Ormer
)

func init() {
	orm.Debug = true
	odbc := conf.GetString("MySqlCon")

	orm.RegisterDataBase("default", "mysql", odbc, 30)

	// register model
	orm.RegisterModel(new(User))
	orm.RegisterModel(new(Role))
	orm.RegisterModel(new(Auth))
	orm.RegisterModel(new(UserRoles))
	orm.RegisterModel(new(Session))

	orm.RegisterModel(new(Resource))
	orm.RegisterModel(new(Matter))
	orm.RegisterModel(new(Binder))
	orm.RegisterModel(new(BindersMap))
	orm.RegisterModel(new(Class))
	orm.RegisterModel(new(Tag))
	orm.RegisterModel(new(TagsMap))
	orm.RegisterModel(new(Category))
	orm.RegisterModel(new(CategorysMap))
	orm.RegisterModel(new(DownloadRecord))
	orm.RegisterModel(new(ConvertRecord))

	orm.RegisterModel(new(Comment))
	orm.RegisterModel(new(Upvote))

	orm.RegisterModel(new(ScoreAmount))
	orm.RegisterModel(new(ScoreTail))

	// create table
	err := orm.RunSyncdb("default", false, true)
	if err != nil {
		fmt.Printf("sync mysql error:%s \n", err)
		os.Exit(1)
	}

	Orm = orm.NewOrm()
}
